import os
from flask import Flask

from FileShare.settings import config
from FileShare.utils import delete_expire_file
from FileShare.extensions import db, login_manager, migrate, scheduler

from FileShare.blueprints.auth import auth_bp
from FileShare.blueprints.main import main_bp
from FileShare.blueprints.files import files_bp
from FileShare.blueprints.loginfo import logs_bp

def create_app(config_name=None):
    if config_name is None:
        config_name = os.getenv('FLASK_CONFIG', 'development')

    app = Flask('FileShare')
    
    app.config.from_object(config[config_name])

    register_extensions(app)
    register_blueprints(app)
    register_commands(app)
    register_errorhandlers(app)
    register_shell_context(app)
    register_template_context(app)

    return app


def register_extensions(app):
    db.init_app(app)
    login_manager.init_app(app)
    migrate.init_app(app)
    scheduler.init_app(app)
    scheduler.add_job(id='delete_file_func',func=delete_expire_file, args=(app.config['FILE_EXPIRE_DATE'], app.config['UPLOAD_FOLDER']), trigger='cron', hour='*')
    scheduler.start()

def register_blueprints(app):
    app.register_blueprint(main_bp)
    app.register_blueprint(auth_bp, url_prefix='/auth')
    app.register_blueprint(files_bp, url_prefix='/file')
    app.register_blueprint(logs_bp, url_prefix='/logs')


def register_shell_context(app):
    pass

def register_template_context(app):
    pass


def register_errorhandlers(app):
    pass


def register_commands(app):
    pass